package com.njlime.member.service.app;

import com.njlime.member.entiies.MemberTrack;
import com.njlime.member.repository.app.AppMemberTrackDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * Created by Dean on 2017/10/9.
 */
@Service
public class AppMemberTrackService {
    @Resource
    private AppMemberTrackDao appMemberTrackDao;
    @Resource
    private AppMemberService appMemberService;
    /**
     * 记录添加
     */
    @Transactional(readOnly = false)
    public void save(MemberTrack memberTrack){
        appMemberTrackDao.save(memberTrack);
    }

    /**
     * 保存记录并更新用户数据
     * @param uid
     * @param businessType
     * @param businessId
     */
    @Transactional(readOnly = false)
    public void save(Integer uid, Integer businessType, Integer businessId){
        if(!isRead(uid,businessType,businessId)){
            //更新vip阅读量信息
            appMemberService.updateReadCount(uid);
            //判断vip升级信息
            appMemberService.upgradeMember(uid);
        }
        MemberTrack vipTrack = new MemberTrack();
        vipTrack.setMember_id(uid);
        vipTrack.setBusiness_id(businessId);
        vipTrack.setBusiness_type(businessType);
        vipTrack.setCreate_time(new Date());
        save(vipTrack);
    }

    /**
     * 判断当前用户是否已经阅读当前内容
     */
    public boolean isRead(Integer uid, Integer businessType, Integer businessId){
        Map<String, Object> bean = appMemberTrackDao.getTodayTrack(uid, businessType, businessId);
        if(bean == null){
            return false;
        }
        return true;
    }

    /**
     * 删除历史记录
     * @param id
     */
    @Transactional
    public void del(int id){
        appMemberTrackDao.delete(id);
    }



}
